package com.zjzywl.smoke.controller;

import com.zjzywl.smoke.config.AppConfig;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zjzywl.smoke.entity.Authority;
import com.zjzywl.smoke.entity.Role;
import com.zjzywl.smoke.util.WebResult;

@Controller
@RequestMapping(value = "/role")
public class RoleActionController extends BaseController {
	@Autowired
	private AppConfig appConfig;

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}

	/*
	 * 获取角色列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public WebResult sysUserList() {
		ResponseEntity<Object> role = appConfig.restTemplate().getForEntity(appConfig.getGetDataIp() + "/role/list",
		        Object.class);
		return WebResult.OK(role);
	}

	// 获取单个角色
	@RequestMapping(value = "/get/{roleId}")
	@ResponseBody
	public WebResult findRole(@PathVariable("roleId") String roleId) {
		ResponseEntity<Authority> role = appConfig.restTemplate()
				.getForEntity(appConfig.getGetDataIp() + "/role/get/" + roleId, Authority.class);
		return WebResult.OK(role);
	}

	// 创建角色
	@RequestMapping(value = "/save")
	@ResponseBody
	public WebResult findRole(Authority authority, @PathParam("roleName") String roleName,
			@PathParam("creator") String creator, @PathParam("creationTime") Date creationTime) throws ParseException {
		ResponseEntity<Role> role = appConfig.restTemplate().postForEntity(appConfig.getGetDataIp()
				+ "/role/save?roleName=" + roleName + "&creator=" + creator + "&creationTime=" + creationTime,
				authority, Role.class);
		return WebResult.OK(role);
	}

	// 修改角色信息
	@RequestMapping(value = "/update")
	@ResponseBody
	public WebResult updateRole(Authority authority, @PathParam("roleName") String roleName,
			@PathParam("roleId") String roleId, @PathParam("creator") String creator,
			@PathParam("creationTime") Date creationTime) {
		ResponseEntity<Role> role = appConfig.restTemplate()
				.postForEntity(appConfig.getGetDataIp() + "/role/update?roleName=" + roleName + "&roleId=" + roleId
						+ "&creator=" + creator + "&creationTime=" + creationTime, authority, Role.class);
		return WebResult.OK(role);
	}

	// 删除角色信息
	@PostMapping(value = "/delect/{roleId}")
	@ResponseBody
	public void delectRole(@PathVariable("roleId") int roleId) {
		ResponseEntity<Role> role = appConfig.restTemplate()
				.postForEntity(appConfig.getGetDataIp() + "/role/delect/" + roleId, null, Role.class);
	}
	//角色唯一性验证
    @RequestMapping(value="/validateName")
    @ResponseBody
    public ResponseEntity<String> checkUserName(@PathParam("roleName") String roleName,
            @PathParam("originalRoleName") String originalRoleName) {
        ResponseEntity<String> users = appConfig.restTemplate()
                .postForEntity(appConfig.getGetDataIp() + "/role/validateName?roleName=" + roleName+"&originalRoleName="+originalRoleName, null, String.class);
        return users;
        

    }
}
